package windows;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import data.Angajat;
import data.Scoala;

public class ModifyAngajatiFrame {

	private JFrame frame;
	private JTable tabelAngajati;
	private JButton modificaAngajat = new JButton("Save");
	private Object[][] data;
	private boolean openedFirstTime = true;
	private Integer[] newSelectedCell = new Integer[2];
	private Integer[] oldSelectedCell = new Integer[2];
	String cellText="";
	public int keyCode=-5;

	public Object[][] getData() {
		return data;
	}

	public JFrame getFrame() {
		return frame;
	}

	public Integer[] getNewSelectedCell() {
		return newSelectedCell;
	}

	public Integer[] getOldSelectedCell() {
		return oldSelectedCell;
	}

	public String getCellText() {
		return cellText;
	}

	public JButton getModificaAngajat() {
		return modificaAngajat;
	}

	public JTable getTabelAngajati() {
		return tabelAngajati;
	}

	public boolean isOpenedFirstTime() {
		return openedFirstTime;
	}

	public void setData(Object[][] data) {
		this.data = data;
	}

	public void setframe(Scoala scoala) {
		JPanel buttonPanel = new JPanel();
		JScrollPane tablePanel = new JScrollPane();
		int countRows = 0;
		frame = new JFrame("Modifica date angajati");
		// modificaAngajat = new JButton("Save");
		modificaAngajat.setActionCommand("saveModify");

		modificaAngajat.setPreferredSize(new Dimension(120, 25));

		data = new Object[scoala.getListaAngajati().size()][4];

		for (Angajat worker : scoala.getListaAngajati()) {
			data[countRows][0] = worker.getNumeAngajat();
			data[countRows][1] = worker.getContAngajat();
			data[countRows][2] = worker.getSuma();
			data[countRows][3] = worker.getBancaAngajat();
			countRows++;
		}

		Object[] numeCol = { "Nume angajat", "Cont", "Suma", "Banca" };
		TableModel model = new DefaultTableModel(data, numeCol);
		tabelAngajati = new JTable(model);

		tabelAngajati.setCellSelectionEnabled(true);

		buttonPanel.add(modificaAngajat);
		tablePanel.getViewport().add(tabelAngajati);

		newSelectedCell[0]=0;
		newSelectedCell[1]=0;
		oldSelectedCell[0]=0;
		oldSelectedCell[1]=1;
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(tablePanel, BorderLayout.CENTER);
		frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
		frame.setSize(800, 600);
		frame.setVisible(true);

	}

	public void setOpenedFirstTime(boolean openedFirstTime) {
		this.openedFirstTime = openedFirstTime;
	}

	public void showFrame() {
		frame.setVisible(true);
	}
	public void setTableText(KeyEvent arg0) {
		if(!isEqualCells())
		{
			cellText="";
		}
	
		oldSelectedCell[0]=newSelectedCell[0];
		oldSelectedCell[1]=newSelectedCell[1];
		if(arg0.getID()==KeyEvent.KEY_TYPED)
			if(keyCode!=10)
			{	cellText+=arg0.getKeyChar();
		Object obj=cellText;
		
		
			

	tabelAngajati.getCellEditor().stopCellEditing();
	tabelAngajati.setValueAt(obj, newSelectedCell[0], newSelectedCell[1]);
		}}
			
		
		private boolean isEqualCells() {
			boolean ret=true;
			if(oldSelectedCell[0]!=newSelectedCell[0]) ret=false;
			if(oldSelectedCell[1]!=newSelectedCell[1]) ret=false;
			return ret;
		}

		public void setTableText(ListSelectionEvent e) {
			if(!isEqualCells())
			{
				cellText="";
			}
	
			oldSelectedCell[0]=newSelectedCell[0];
			oldSelectedCell[1]=newSelectedCell[1];
			
		}
}
